package com.win.cn.core.model;

import com.win.cn.util.DateUtil;
import com.win.cn.util.StringUtil;

import javafx.collections.FXCollections;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;

/**
 * 时间和UTC时间转换实现
 * @author wangzh
 * 2018年6月9日 上午10:10:33
 */
public class TimeAndUTC {
	
	public void utcTotime(GridPane grid){
		TextField inputText = new TextField();
		TextField outputText = new TextField();		
		//下拉框
		ChoiceBox<Object> choiceBox=new ChoiceBox<>();
		choiceBox.setItems(FXCollections.observableArrayList("秒级", "毫秒级"));
		choiceBox.getSelectionModel().select(0);
		choiceBox.setTooltip(new Tooltip("选择秒或毫秒")); 
		
		Button UTCToTime=new Button("转换");
		UTCToTime.setOnAction( event -> {
			String text=inputText.getText();
			if(StringUtil.isEmpty(text)){
				
			}else{
				if(!StringUtil.isNumber(text)){
					return;
				}else{
					if(choiceBox.getSelectionModel().getSelectedIndex()==1){
						outputText.setText(DateUtil.getDataDetail(text));
					}else{
						outputText.setText(DateUtil.UTCTodate(text));
					}
				}
				
			}
			
		});
		
		if(choiceBox.getSelectionModel().getSelectedIndex()==1){
			inputText.setText(DateUtil.formatStrToDayFirstSec(DateUtil.getTime())+"");
		}else{
			inputText.setText(DateUtil.dateToUTC(DateUtil.getTime())+"");
		}
		outputText.setText(DateUtil.getTime());
		
		int row=0;
		grid.add(new Label("时间戳："), 0, row);
	    grid.add(inputText, 1, row);
	    grid.add(choiceBox, 2, row);
	    grid.add(UTCToTime, 3, row);
	    grid.add(new Label("时间："), 4, row);
	    grid.add(outputText, 5, row);
	}
	
	public void timeToutc(GridPane grid){
		TextField inputText = new TextField();
		TextField outputText = new TextField();		
		//下拉框
		ChoiceBox<Object> choiceBox=new ChoiceBox<>();
		choiceBox.setItems(FXCollections.observableArrayList("秒级", "毫秒级"));
		choiceBox.getSelectionModel().select(0);
		choiceBox.setTooltip(new Tooltip("选择秒或毫秒")); 
		
		Button UTCToTime=new Button("转换");
		UTCToTime.setOnAction( event -> {
			String text=inputText.getText();
			if(StringUtil.isEmpty(text)){
				
			}else{
				if(choiceBox.getSelectionModel().getSelectedIndex()==1){
					outputText.setText(DateUtil.formatStrToDayFirstSec(text));
				}else{
					outputText.setText(DateUtil.dateToUTC(text));
				}
			}
			
		});
		
		if(choiceBox.getSelectionModel().getSelectedIndex()==1){
			outputText.setText(DateUtil.formatStrToDayFirstSec(DateUtil.getTime())+"");
		}else{
			outputText.setText(DateUtil.dateToUTC(DateUtil.getTime())+"");
		}
		inputText.setText(DateUtil.getTime());
		
		int row=3;
	    grid.add(new Label("时间："), 0, row);
	    grid.add(inputText, 1, row);
	    grid.add(choiceBox, 2, row);
	    grid.add(UTCToTime, 3, row);
	    grid.add(new Label("时间戳："), 4, row);
	    grid.add(outputText, 5, row);
	}

}
